﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace Ornithopter.FlashDiskHelper
{
    public class LogicalDisk : IDisposable
    {
        public USBStorage OwnerDevice { get; private set; }
        public LogicalDiskProperties Properties { get; private set; }
        private ManagementObject mLogicalDisk;

        public LogicalDisk(ManagementObject mLogicalDisk, USBStorage owner)
        {
            OwnerDevice = owner;
            this.mLogicalDisk = mLogicalDisk;
            Properties = new LogicalDiskProperties();
            try
            {
                Properties.VolumName = mLogicalDisk.Properties["VolumeName"].Value.ToString();
                Properties.Caption = mLogicalDisk.Properties["Caption"].Value.ToString();
                UInt64 x = 0;
                UInt64.TryParse(mLogicalDisk.Properties["FreeSpace"].Value.ToString(), out x);
                Properties.FreeSpace = x;
                UInt64.TryParse(mLogicalDisk.Properties["Size"].Value.ToString(), out x);
                Properties.Size = x;
            }
            catch { }
        }

        public override string ToString()
        {
            return String.Format("{0}-{1},可用空间:{2}"
                    , Properties.VolumName
                    , Properties.Caption
                    , Properties.FreeSpace);
        }

        public void Dispose()
        {
            if (mLogicalDisk != null)
                mLogicalDisk.Dispose();
        }
    }
}
